package org.oexam.ostudy.sl;

import java.util.List;

import org.oexam.ostudy.db.UsersAccess;
import org.oexam.ostudy.facade.struct.TeacherDetails;
import org.oexam.ostudy.facade.struct.TeacherList;
import org.oexam.ostudy.struct.Students;
import org.oexam.ostudy.struct.Teachers;
import org.oexam.ostudy.struct.Users;

public class ManageTeacherList {
	
	private UsersAccess details = new UsersAccess();
	
	public TeacherList getTeacherList(){
		
	    TeacherList list = new TeacherList();
		
      
		
		List<Teachers> teacherList = details.getTeachers();
		list.setTeacherList(teacherList);
		
		return list;
	}
	
	public TeacherDetails getTeacherDetails(int teacher_id) {
		
		
		Teachers teacher = details.getTeacher(teacher_id);
		
		if (teacher == null) {
			return null;
		}
		
		int user_id = teacher.getUser_id();
		Users user = details.getUser(user_id); 
		
		if (user == null) {
			return null;
		}
		
		TeacherDetails td= new TeacherDetails();
		td.setFirstName(teacher.getFirst_name());
		td.setLastName(teacher.getLast_name());
		td.setUserName(user.getUser_name());
		td.setPassword(user.getPassword());
		td.setUser_id(user.getUser_id());
		td.setTeacher_id(teacher.getTeacher_id());
		td.setAddress(teacher.getAddress());
		td.setEmail(teacher.getEmail());
		td.setPhone(teacher.getPhone());
		
		
		
		
		return td;
	}
	
	public boolean deleteTeacher(int teacher_id) {
		
		
		Teachers teacher = details.getTeacher(teacher_id);
		
		if (teacher == null) {
			return false;
		}
		
		int user_id = teacher.getUser_id();
		Users user = details.getUser(user_id); 
		
		
		
		if (user == null) {
			return false;
		}
		
		boolean isTeacherResponsible = checkIsTeacherResponsible(teacher_id);
		
		if(isTeacherResponsible) {
			
			return false;
		}
		
		details.deleteTeacher(teacher, user);
		
		return true;
	}
	
	private boolean checkIsTeacherResponsible(int teacher_id) {
		
		List<Students> students = details.getStudents();
		
		
		for (Students student : students) {
			 int id = student.getTeacher_responsible();
			 if(teacher_id == id) {			
					return true;
			 }
		}
		
		return false;
		
	}
	

}
